Skip to content

Method: lambda$findMedia$0(Id, String)

1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * NorthernWind - lightweight CMS
5: * http://tidalwave.it/projects/northernwind
6: *
7: * Copyright (C) 2011 - 2025 Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/northernwind-src
22: * git clone https://github.com/tidalwave-it/northernwind-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.northernwind.frontend.media.impl;
27:
28: import javax.annotation.Nonnull;
29: import javax.inject.Inject;
30: import javax.inject.Provider;
31: import java.util.Optional;
32: import java.io.IOException;
33: import it.tidalwave.util.Id;
34: import it.tidalwave.util.NotFoundException;
35: import it.tidalwave.image.EditableImage;
36: import it.tidalwave.image.op.ReadOp;
37: import it.tidalwave.northernwind.core.model.Media;
38: import it.tidalwave.northernwind.core.model.Resource;
39: import it.tidalwave.northernwind.core.model.ResourceFile;
40: import it.tidalwave.northernwind.core.model.ResourceProperties;
41: import it.tidalwave.northernwind.core.model.SiteProvider;
42: import lombok.extern.slf4j.Slf4j;
43: import static it.tidalwave.image.op.ReadOp.Type.METADATA;
44: import static java.util.Collections.emptyList;
45: import static it.tidalwave.northernwind.core.model.Media._Media_;
46: import static it.tidalwave.northernwind.frontend.media.impl.EmbeddedMediaMetadataProvider.*;
47:
48: /***************************************************************************************************************************************************************
49: *
50: * The default implementation of {@link MetadataLoader}.
51: *
52: * @author Fabrizio Giudici
53: *
54: **************************************************************************************************************************************************************/
55: @Slf4j
56: public class DefaultMetadataLoader implements MetadataLoader
57: {
58: @Inject
59: private Provider<SiteProvider> siteProvider;
60:
61: /***********************************************************************************************************************************************************
62: * {@inheritDoc}
63: **********************************************************************************************************************************************************/
64: @Override @Nonnull
65: public ResourceFile findMediaResourceFile (@Nonnull final ResourceProperties siteNodeProperties,
66: @Nonnull final Id mediaId)
67: throws NotFoundException
68: {
69: final var properties = siteNodeProperties.getGroup(P_GROUP_ID);
70: return findMedia(mediaId, properties).map(Resource::getFile).orElseThrow(NotFoundException::new); // FIXME
71: }
72:
73: /***********************************************************************************************************************************************************
74: * {@inheritDoc}
75: **********************************************************************************************************************************************************/
76: @Override @Nonnull
77: public Metadata loadMetadata (@Nonnull final ResourceFile file)
78: throws IOException
79: {
80: log.debug("loadMetadata({})", file.getPath());
81: final var image = EditableImage.create(new ReadOp(file.toFile(), METADATA));
82: return new DefaultMetadata(file.getName(), image);
83: }
84:
85: /***********************************************************************************************************************************************************
86: * Finds a {@link Media} item for the given id.
87: *
88: * @param mediaId the media id
89: * @param properties the configuration properties
90: * @return the {@code Media}
91: **********************************************************************************************************************************************************/
92: @Nonnull
93: private Optional<Media> findMedia (@Nonnull final Id mediaId, @Nonnull final ResourceProperties properties)
94: {
95: final var site = siteProvider.get().getSite();
96:
97: return properties.getProperty(P_MEDIA_PATHS).orElse(emptyList())
98: .stream()
99: .map(pathTemplate -> String.format(pathTemplate, mediaId.stringValue()))
100: .flatMap(path -> site.find(_Media_).withRelativePath(path).stream())
101: .findFirst();
102: }
103: }